﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ModelPredictPro.Predict.SVM;
using System.Linq;

namespace ModelPredictPro.WindowForms.UserControls {
    public partial class UCTaskView : UserControl {
        public UCTaskView() {
            InitializeComponent();
            listBoxTaskList.ListBox.DataSource = TaskList;
        }

        BindingList<ITask> TaskList = new BindingList<ITask>();

        public void Add(ITask task) {
            TaskList.Add(task);
        }

        private void btnRunTasks_Click(object sender, EventArgs e) {
            UCConsole view = this.Tag as UCConsole;
            new Action(() => {
                int i = 0;
                DateTime allstart = DateTime.Now;
                foreach (var item in TaskList) {
                    DateTime start = DateTime.Now;
                    view.InvokeWriteLine(string.Format("Task #{0} ", ++i));
                    item.Run();
                    TimeSpan span = DateTime.Now - start;
                    view.InvokeWriteLine(string.Format("Task #{0} completed, cosumed time : {1}", i, view.ConvertToString(span)));
                    view.InvokeWriteLine("");
                }
                view.InvokeWriteLine("");
                view.InvokeWriteLine(string.Format("All {0} task(s) completed, cosumed time : {1}", TaskList.Count, view.ConvertToString(DateTime.Now - allstart)));
            }).BeginInvoke(OnTaskCompleted, null);
        }
        void OnTaskCompleted(IAsyncResult async) {
            this.Invoke(new Action(() =>
            MessageBox.Show(string.Format("{0} tasks completed !", TaskList.Count), "Success", MessageBoxButtons.OK,
                MessageBoxIcon.Information)));

        }
    }
}
